#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Debug.h"
#include "xmlsock.h"

// socket  1
// file    2
// hash    3
#define XML_SOCKET_VERSION 3

#if (1 == XML_SOCKET_VERSION)
#include "xmlsock_s.h"
#endif

#include "xmlfile_s.h"
#include "xmlhash_s.h"


int xml_init(int nDevType)
{
    Safe_Printf("xml.version=%d", XML_SOCKET_VERSION);
#if (1 == XML_SOCKET_VERSION)
    xmlsoc_init(nDevType);
#elif (2 == XML_SOCKET_VERSION)
    xmlfile_init(nDevType);
#elif (3 == XML_SOCKET_VERSION)
    xmlhash_init(nDevType);
#endif
    return 0;
}

int  xml_gets(const char *xmlfile, const char *nodepath, char *szValue, int nLen, const char *szDefaultValue)
{
    int ret = -1;
#if (1 == XML_SOCKET_VERSION)
    ret = xmlsoc_gets(xmlfile, nodepath, szValue, nLen, szDefaultValue);
#elif (2 == XML_SOCKET_VERSION)
    ret = xmlfile_gets(xmlfile, nodepath, szValue, nLen, szDefaultValue);
#elif (3 == XML_SOCKET_VERSION)
    ret = xmlhash_gets(xmlfile, nodepath, szValue, nLen, szDefaultValue);
#endif
    return ret;
}

int xml_sets(const char *xmlfile, const char *nodepath, char *szValue, int nSaveDelay)
{
    int ret = -1;
#if (1 == XML_SOCKET_VERSION)
    ret = xmlsoc_sets(xmlfile, nodepath, szValue, nSaveDelay);
#elif (2 == XML_SOCKET_VERSION)
    ret = xmlfile_sets(xmlfile, nodepath, szValue, nSaveDelay);
#elif (3 == XML_SOCKET_VERSION)
    ret = xmlhash_sets(xmlfile, nodepath, szValue, nSaveDelay);
#endif
    return ret;
}

int xml_getn(const char *xmlfile, const char *nodepath, int *pValue, int *pDefaultValue)
{
    int ret = -1;
#if (3 == XML_SOCKET_VERSION)
    ret = xmlhash_getn(xmlfile, nodepath, pValue, pDefaultValue);
#endif
    return ret;
}

int xml_setn(const char *xmlfile, const char *nodepath, int nValue, int nSaveDelay)
{
    int ret = -1;
#if (3 == XML_SOCKET_VERSION)
    ret = xmlhash_setn(xmlfile, nodepath, nValue, nSaveDelay);
#endif
    return ret;
}


int xml_getattr(const char *xmlfile, const char *nodepath, const char *szAttrName, char *szAttrValue, int nLen)
{
    int ret = -1;
#if (3 == XML_SOCKET_VERSION)
    ret = xmlhash_getattr(xmlfile, nodepath, szAttrName, szAttrValue, nLen);
#endif
    return ret;
}

int xml_setattr(const char *xmlfile, const char *nodepath, const char *szAttrName, char *szAttrValue, int nSaveDelay)
{
    int ret = -1;
#if (3 == XML_SOCKET_VERSION)
    ret = xmlhash_setattr(xmlfile, nodepath, szAttrName, szAttrValue, nSaveDelay);
#endif
    return ret;
}

int  xml_replace(const char *szXmlfile_Old, const char *xmlfile_tmp, const char *xmlfile_config)
{
    int ret = -1;
#if (1 == XML_SOCKET_VERSION)
    ret = xmlsoc_replace(szXmlfile_Old, xmlfile_tmp, xmlfile_config);
#elif (2 == XML_SOCKET_VERSION)
    ret = xmlfile_replace(szXmlfile_Old, xmlfile_tmp, xmlfile_config);
#elif (3 == XML_SOCKET_VERSION)
    ret = xmlhash_replace(szXmlfile_Old, xmlfile_tmp, xmlfile_config);
#endif
    return ret;
}

int xml_reload()
{
    int ret = -1;
#if (3 == XML_SOCKET_VERSION)
    ret = xmlhash_reload();
#endif
    return ret;
}


